package com.sh1604.duitang.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bartoszlipinski.recyclerviewheader2.RecyclerViewHeader;
import com.google.gson.Gson;
import com.sh1604.duitang.R;
import com.sh1604.duitang.activity.AtricleActivity;
import com.sh1604.duitang.activity.HotPhotoActivity;
import com.sh1604.duitang.activity.ImagedetailsActivity;
import com.sh1604.duitang.adapter.MyDataAdapter;
import com.sh1604.duitang.constant.UrlString;
import com.sh1604.duitang.mybean.FindImageBean;
import com.sh1604.duitang.mybean.ImageBean;
import com.sh1604.duitang.utils.OKHttpUtils;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 */
public class FindFragment extends Fragment {

    @InjectView(R.id.search_view)
    SearchView searchView;
    @InjectView(R.id.find_tv1)
    TextView findTv1;
    @InjectView(R.id.toolbar)
    Toolbar toolbar;
    @InjectView(R.id.item_top_ll1)
    LinearLayout itemTopLl1;
    @InjectView(R.id.item_top_ll2)
    LinearLayout itemTopLl2;
    @InjectView(R.id.second_ll1)
    LinearLayout secondLl1;
    @InjectView(R.id.second_ll2)
    LinearLayout secondLl2;
    @InjectView(R.id.second_ll3)
    LinearLayout secondLl3;
    @InjectView(R.id.view_find_image)
    ImageView viewFindImage;
    private Activity mActivity;
    private ArrayList<ImageBean.DataBean.GroupItemsBean> mDatas;
    private RecyclerViewHeader header;
    private RecyclerView recyclerView;
    private MyDataAdapter adapter;
    private LinearLayoutManager manager;
    private View rootView;

    public FindFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        mDatas = new ArrayList<>();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
//        if (rootView==null){
//            rootView = inflater.inflate(R.layout.fragment_find,null);
//        }
//        ViewGroup group = (ViewGroup) rootView.getParent();
//        if (group!=null){
//            group.removeView(rootView);
//        }
//        return rootView;
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        ButterKnife.inject(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        header = (RecyclerViewHeader) view.findViewById(R.id.header);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclre1);
        adapter = new MyDataAdapter(mActivity, mDatas);
        recyclerView.setAdapter(adapter);
       // manager = new LinearLayoutManager(mActivity,LinearLayoutManager.VERTICAL,false);
        manager = new GridLayoutManager(mActivity, 2, GridLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        header.attachTo(recyclerView);

        OKHttpUtils.newInstance(getContext()).getAsyncMainData(UrlString.URL_FIND, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                final ImageBean bean = new Gson().fromJson(result, ImageBean.class);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        for (int i = 0; i < bean.getData().size(); i++) {
                            mDatas.addAll(bean.getData().get(i).getGroup_items());
                        }
                        Log.i("mDatas", "--->--->!!!--->--->" + mDatas.size());
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
        OKHttpUtils.newInstance(mActivity).getAsyncMainData(UrlString.URL_Image, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                final FindImageBean bean1 = new Gson().fromJson(result, FindImageBean.class);
                mHandler1.post(new Runnable() {
                    @Override
                    public void run() {
                        Picasso.with(getContext()).load(bean1.getData().getObject_list().get(0).getImage_url()).into(viewFindImage);
                    }
                });
            }
        });
    }

    Handler mHandler = new Handler();
    Handler mHandler1 = new Handler();

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);

    }

    @OnClick({R.id.search_view, R.id.find_tv1, R.id.toolbar, R.id.item_top_ll1, R.id.item_top_ll2, R.id.second_ll1, R.id.second_ll2, R.id.second_ll3, R.id.view_find_image})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_view:
                break;
            case R.id.find_tv1:
                break;
            case R.id.toolbar:
                break;
            case R.id.item_top_ll1:
                break;
            case R.id.item_top_ll2:
                break;
            case R.id.second_ll1:
                startActivity(new Intent(mActivity,HotPhotoActivity.class));
                break;
            case R.id.second_ll2:
                startActivity(new Intent(mActivity, AtricleActivity.class));
                break;
            case R.id.second_ll3:
                break;
            case R.id.view_find_image:
                startActivity(new Intent(mActivity, ImagedetailsActivity.class));
                break;
        }
    }
}
